﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Reflection;

namespace Rockfan
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;

            using (var game = new RockfanGame())
                game.Run();
        }

        static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
        {
            var assemblyName = args.Name;
            var executingAssembly = Assembly.GetExecutingAssembly();

            var resources = executingAssembly.GetManifestResourceNames().Where(n => n.EndsWith(".dll"));

            foreach (var resource in resources)
            {
                using (var stream = executingAssembly.GetManifestResourceStream(resource))
                {
                    if (stream == null)
                        continue;

                    var data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);

                    return Assembly.Load(data);
                }
            }

            return null;
        }

        [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern IntPtr LoadLibrary(string lpFileName);
    }
}
